home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / ZIP_1_0.lha / src / dosio.c < prev    next >
C/C++ Source or Header  |  1992-10-13  |  9KB  |  496 lines

  1. /*
  2.  * dosio.c
  3.  *
  4.  * Generic DOS screen I/O functions. Uses ANSI.SYS
  5.  *
  6.  * Mark Howell 28-Jul-1992 V1.0
  7.  *
  8.  */
  9.  
  10. #include "ztypes.h"
  11.  
  12. #ifdef __STDC__
  13. static int dos_getch (void);
  14. static void bios_scroll (unsigned char, unsigned char, unsigned char,
  15.                          unsigned char, unsigned char, unsigned char);
  16. #else
  17. static int dos_getch ();
  18. static void bios_scroll ();
  19. #endif
  20.  
  21. static int cursor_saved = OFF;
  22.  
  23. #ifdef __STDC__
  24. void fatal (const char *s)
  25. #else
  26. void fatal (s)
  27. const char *s;
  28. #endif
  29. {
  30.  
  31.     reset_screen ();
  32.     printf ("\nFatal error: %s (PC = %lx)\n", s, pc);
  33.     exit (1);
  34.  
  35. }/* fatal */
  36.  
  37. #ifdef __STDC__
  38. void initialize_screen (void)
  39. #else
  40. void initialize_screen ()
  41. #endif
  42. {
  43.  
  44.     screen_rows = 25;
  45.     screen_cols = 80;
  46.     set_attribute (NORMAL);
  47.     clear_screen ();
  48.     move_cursor (screen_rows, 1);
  49.     move_cursor (screen_rows / 2, (screen_cols - sizeof ("The story is loading...")) / 2);
  50.     fputs ("The story is loading...", stdout);
  51.  
  52. }/* initialize_screen */
  53.  
  54. #ifdef __STDC__
  55. void restart_screen (void)
  56. #else
  57. void restart_screen ()
  58. #endif
  59. {
  60.  
  61.     set_attribute (NORMAL);
  62.     clear_screen ();
  63.     move_cursor (screen_rows, 1);
  64.  
  65. }/* restart_screen */
  66.  
  67. #ifdef __STDC__
  68. void reset_screen (void)
  69. #else
  70. void reset_screen ()
  71. #endif
  72. {
  73.  
  74.     set_attribute (NONE);
  75.  
  76. }/* reset_screen */
  77.  
  78. #ifdef __STDC__
  79. void create_status_window (void)
  80. #else
  81. void create_status_window ()
  82. #endif
  83. {
  84.  
  85. }/* create_status_window */
  86.  
  87. #ifdef __STDC__
  88. void delete_status_window (void)
  89. #else
  90. void delete_status_window ()
  91. #endif
  92. {
  93.  
  94. }/* delete_status_window */
  95.  
  96. #ifdef __STDC__
  97. void select_status_window (void)
  98. #else
  99. void select_status_window ()
  100. #endif
  101. {
  102.  
  103.     save_cursor_position ();
  104.     move_cursor (1, 1);
  105.  
  106. }/* select_status_window */
  107.  
  108. #ifdef __STDC__
  109. void select_text_window (void)
  110. #else
  111. void select_text_window ()
  112. #endif
  113. {
  114.  
  115.     restore_cursor_position ();
  116.  
  117. }/* select_text_window */
  118.  
  119. #ifdef __STDC__
  120. void clear_screen (void)
  121. #else
  122. void clear_screen ()
  123. #endif
  124. {
  125.  
  126.     fputs ("\033[2J", stdout);
  127.  
  128. }/* clear_screen */
  129.  
  130. #ifdef __STDC__
  131. void clear_line (void)
  132. #else
  133. void clear_line ()
  134. #endif
  135. {
  136.  
  137.     fputs ("\033[K", stdout);
  138.  
  139. }/* clear_line */
  140.  
  141. #ifdef __STDC__
  142. void clear_text_window (void)
  143. #else
  144. void clear_text_window ()
  145. #endif
  146. {
  147.     unsigned char ur, uc, lr, lc;
  148.  
  149.     ur = (unsigned char) (status_size + 1);
  150.     uc = 1;
  151.     lr = (unsigned char) screen_rows;
  152.     lc = (unsigned char) screen_cols;
  153.     bios_scroll (0, ur, uc, lr, lc, 0x17);
  154.  
  155. }/* clear_text_window */
  156.  
  157. #ifdef __STDC__
  158. void clear_status_window (void)
  159. #else
  160. void clear_status_window ()
  161. #endif
  162. {
  163.     unsigned char ur, uc, lr, lc;
  164.  
  165.     ur = 1;
  166.     uc = 1;
  167.     lr = (unsigned char) status_size;
  168.     lc = (unsigned char) screen_cols;
  169.     bios_scroll (0, ur, uc, lr, lc, 0x17);
  170.  
  171.     move_cursor (1, 1);
  172.  
  173. }/* clear_status_window */
  174.  
  175. #ifdef __STDC__
  176. void move_cursor (int row, int col)
  177. #else
  178. void move_cursor (row, col)
  179. int row;
  180. int col;
  181. #endif
  182. {
  183.  
  184.     printf ("\033[%d;%dH", row, col);
  185.  
  186. }/* move_cursor */
  187.  
  188. #ifdef __STDC__
  189. void save_cursor_position (void)
  190. #else
  191. void save_cursor_position ()
  192. #endif
  193. {
  194.  
  195.     if (cursor_saved == OFF) {
  196.         fputs ("\033[s", stdout);
  197.         cursor_saved = ON;
  198.     }
  199.  
  200. }/* save_cursor_position */
  201.  
  202. #ifdef __STDC__
  203. void restore_cursor_position (void)
  204. #else
  205. void restore_cursor_position ()
  206. #endif
  207. {
  208.  
  209.     if (cursor_saved == ON) {
  210.         fputs ("\033[u", stdout);
  211.         cursor_saved = OFF;
  212.     }
  213.  
  214. }/* restore_cursor_position */
  215.  
  216. #ifdef __STDC__
  217. void set_attribute (int attribute)
  218. #else
  219. void set_attribute (attribute)
  220. int attribute;
  221. #endif
  222. {
  223.  
  224.     switch (attribute) {
  225.         case NORMAL:     fputs ("\033[0m\033[37;44m", stdout); break;
  226.         case REVERSE:    fputs ("\033[0m\033[34;47m", stdout); break;
  227.         case BOLD:       fputs ("\033[0m\033[37;44m\033[1m", stdout); break;
  228.         case BLINK:      fputs ("\033[0m\033[37;44m", stdout); break;
  229.         case UNDERSCORE: fputs ("\033[0m\033[31;47m", stdout); break;
  230.  
  231.         default:         fputs ("\033[0m", stdout);
  232.     }
  233.  
  234. }/* set_attribute */
  235.  
  236. #ifdef __STDC__
  237. void display_char (int c)
  238. #else
  239. void display_char (c)
  240. int c;
  241. #endif
  242. {
  243.  
  244.     putchar (c);
  245.  
  246. }/* display_char */
  247.  
  248. #ifdef __STDC__
  249. void input_line (void)
  250. #else
  251. void input_line ()
  252. #endif
  253. {
  254.     char c, *cp;
  255.  
  256.     input[1] = 0;
  257.     cp = &input[2];
  258.     for ( ; ; ) {
  259.         c = input_character ();
  260.         if (c == '\x00e' || c == '\a')
  261.             sound (1);
  262.         else if (c == '\r') {
  263.             *cp++ = c;
  264.             output_char (c);
  265.             scroll_line ();
  266.             return;
  267.         } else if (c == '\x07f' || c == '\b' || c == '\x00b') {
  268.             if (input[1] == 0)
  269.                 sound (1);
  270.             else {
  271.                 input[1]--;
  272.                 cp--;
  273.                 fputs ("\b \b", stdout);
  274.             }
  275.         } else
  276.             if (input[1] == (input[0] - (char) 2))
  277.                 sound (1);
  278.             else {
  279.                 input[1]++;
  280.                 *cp++ = c;
  281.                 output_char (c);
  282.             }
  283.     }
  284.  
  285. }/* input_line */
  286.  
  287. #ifdef __STDC__
  288. char input_character (void)
  289. #else
  290. char input_character ()
  291. #endif
  292. {
  293.     int c;
  294.  
  295.     for ( ; ; ) {
  296.         c = dos_getch () & 0x7f;
  297.         if (c >= ' ' || c == '\b' || c == '\r')
  298.             return ((char) c);
  299.         c = dos_getch ();
  300.         if (c == 'H')
  301.             return ('\x00e');
  302.         if (c == 'K')
  303.             return ('\x00b');
  304.         if (c == 'M')
  305.             return ('\x007');
  306.         if (c == 'P')
  307.             return ('\r');
  308.     }
  309.  
  310. }/* input_character */
  311.  
  312. #ifdef __STDC__
  313. void scroll_line (void)
  314. #else
  315. void scroll_line ()
  316. #endif
  317. {
  318.     unsigned char ur, uc, lr, lc;
  319.  
  320.     ur = (unsigned char) status_size;
  321.     uc = 0;
  322.     lr = (unsigned char) (screen_rows - 1);
  323.     lc = (unsigned char) (screen_cols - 1);
  324.     bios_scroll (1, ur, uc, lr, lc, 0x17);
  325.     putchar ('\r');
  326.  
  327. }/* scroll_line */
  328.  
  329. #ifdef __STDC__
  330. static void bios_scroll (unsigned char num_lines,
  331.                          unsigned char upper_row,
  332.                          unsigned char left_col,
  333.                          unsigned char lower_row,
  334.                          unsigned char right_col,
  335.                          unsigned char filler)
  336. #else
  337. static void bios_scroll (num_lines,
  338.                          upper_row,
  339.                          left_col,
  340.                          lower_row,
  341.                          right_col,
  342.                          filler)
  343. unsigned char num_lines;
  344. unsigned char upper_row;
  345. unsigned char left_col;
  346. unsigned char lower_row;
  347. unsigned char right_col;
  348. unsigned char filler;
  349. #endif
  350. {
  351.  
  352.     _asm {
  353.         mov     al,num_lines;
  354.         mov     ch,upper_row;
  355.         mov     cl,left_col;
  356.         mov     dh,lower_row;
  357.         mov     dl,right_col;
  358.         mov     bh,filler;
  359.  
  360.         mov     ah,06H
  361.         int     10H
  362.     }
  363.  
  364. #if 0
  365. TITLE   Access to scroll BIOS call from C
  366. .MODEL  COMPACT
  367. .CODE
  368. ;_TEXT   SEGMENT WORD PUBLIC 'CODE'
  369.         PUBLIC  _bios_scroll
  370. ;    ASSUME  CS:_TEXT,DS:NOTHING,ES:NOTHING,SS:NOTHING
  371. _bios_scroll PROC
  372.  
  373.         push    bp
  374.         mov     bp,sp
  375.  
  376.         mov     al,[bp+04]
  377.         mov     ch,[bp+06]
  378.         mov     cl,[bp+08]
  379.         mov     dh,[bp+10]
  380.         mov     dl,[bp+12]
  381.         mov     bh,[bp+14]
  382.  
  383.         mov     ah,06H
  384.         int     10H
  385.  
  386.         pop     bp
  387.         ret
  388.  
  389. _bios_scroll ENDP
  390. ;_TEXT   ENDS
  391.         END
  392. #endif /* 0 */
  393.  
  394. }/* bios_scroll */
  395.  
  396. #ifdef __STDC__
  397. static int dos_getch (void)
  398. #else
  399. static int dos_getch ()
  400. #endif
  401. {
  402.  
  403.     _asm {
  404.         mov     ah,07h
  405.         int     21h
  406.  
  407.         mov     ah,00h
  408.     }
  409.  
  410. #if 0
  411. TITLE   Access to getch DOS call from C
  412. .MODEL  COMPACT
  413. .CODE
  414. ;_TEXT   SEGMENT WORD PUBLIC 'CODE'
  415.         PUBLIC  _dos_getch
  416. ;    ASSUME  CS:_TEXT,DS:NOTHING,ES:NOTHING,SS:NOTHING
  417. _dos_getch PROC
  418.  
  419.         push    bp
  420.         mov     bp,sp
  421.  
  422.         mov     ah,07h
  423.         int     21h
  424.  
  425.         mov     ah,00h
  426.  
  427.         pop     bp
  428.         ret
  429.  
  430. _dos_getch ENDP
  431. ;_TEXT   ENDS
  432.         END
  433. #endif /* 0 */
  434.  
  435. }/* dos_getch */
  436.  
  437. /*
  438.  * fit_line
  439.  *
  440.  * This routine determines whether a line of text will still fit
  441.  * on the screen.
  442.  *
  443.  * line : Line of text to test.
  444.  * pos  : Length of text line (in characters).
  445.  * max  : Maximum number of characters to fit on the screen.
  446.  *
  447.  */
  448.  
  449. #ifdef __STDC__
  450. int fit_line (const char *line, int pos, int max)
  451. #else
  452. int fit_line (line, pos, max)
  453. const char *line;
  454. int pos;
  455. int max;
  456. #endif
  457. {
  458.  
  459.     return (pos < max);
  460.  
  461. }/* fit_line */
  462.  
  463. /*
  464.  * print_status
  465.  *
  466.  * Print the status line (type 3 games only).
  467.  *
  468.  * argv[0] : Location name
  469.  * argv[1] : Moves/Time
  470.  * argv[2] : Score
  471.  *
  472.  * Depending on how many arguments are passed to this routine
  473.  * it is to print the status line. The rendering attributes
  474.  * and the status line window will be have been activated
  475.  * when this routine is called. It is to return FALSE if it
  476.  * cannot render the status line in which case the interpreter
  477.  * will use display_char() to render it on its own.
  478.  *
  479.  * This routine has been provided in order to support
  480.  * proportional-spaced fonts.
  481.  *
  482.  */
  483.  
  484. #ifdef __STDC__
  485. int print_status (int argc, char *argv[])
  486. #else
  487. int print_status (argc, argv)
  488. int argc;
  489. char *argv[];
  490. #endif
  491. {
  492.  
  493.     return (FALSE);
  494.  
  495. }/* print_status */
  496.